home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / fractal / kaos.lha / dimslib / dims_fn.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-01-25  |  2.7 KB  |  115 lines

  1. /* Patrick A. Worfolk
  2.    January 20, 1990
  3.    Fractal dimension algorithm functions
  4. */
  5.  
  6. #define MAX_LONG_VALUE 2147483648.0  /* maximum positive integer 2^31 */
  7. #define TRUE 1
  8. #define FALSE 0
  9.  
  10.  
  11. /* CHECK_MAX_SCALES computes scale^n.
  12.    Error checking:  If scale^n is greater than the machine integer
  13.    limits then n is set to the maximum possible value.  If scale is
  14.    greater than MAX_LONG_VALUE then scale is reset to 2.
  15.    */
  16. dims_check_max_scales(pscale,pn)
  17. double *pscale;
  18. int *pn;
  19. {
  20.     int i;
  21.     double max;
  22.  
  23.     max=*pscale;
  24.     if (*pscale>MAX_LONG_VALUE) {
  25.         *pscale = 2.0;
  26.         system_mess_proc(1,
  27.           "Scaling factor is too large.  Defaulted to 2.");
  28.         }
  29.     for (i=1; ((i<*pn) && (max <= MAX_LONG_VALUE)); i++) {
  30.         max *= *pscale;
  31.         }
  32.     if (max > MAX_LONG_VALUE) {
  33.         *pn = i-1;
  34.         system_mess_proc(1,
  35.           "Cannot compute full number of scales accurately.");
  36.         }
  37. }
  38.  
  39.  
  40. /* RESCALE multiplies each element in the data array by factor.
  41.    */
  42. dims_rescale(data,xsize,ysize,factor)
  43. int xsize,ysize;
  44. double **data,factor;
  45. {
  46.     int i,j;
  47.  
  48.     for (i=0; i<xsize; ++i) {
  49.         for (j=0; j<ysize; ++j) {
  50.             data[i][j] *= factor;
  51.             }
  52.         }
  53. }
  54.  
  55.  
  56. /* NORMALIZE converts a double float array into one where every
  57.    point has a value between zero and one.
  58.    Note: the subscripts of the arrays are reversed.
  59.    */
  60. dims_normalize(data,norm_data,length,dimens,max,min)
  61. double **data,**norm_data,*max,*min;
  62. int length,dimens;
  63. {
  64.     int i,j;
  65.     double scale_factor;
  66.  
  67.         for (j=0; j<dimens; j++) {
  68.         if (max[j]==min[j]) scale_factor=0;
  69.                 else scale_factor =  0.999999999999 / (max[j]-min[j]);
  70.                 for (i=0; i<length; i++) {
  71.                        norm_data[i][j] = (data[j][i]-min[j]) * scale_factor;
  72.                         }
  73.                 }
  74. }
  75.  
  76.  
  77.  
  78. /* COUNT counts the number of unique elements in a sorted list.
  79.    Uniqueness is determined only by integral part.
  80.    data[x][y] is considered as a list where x indexes the different
  81.    elements and y indexes the different components of one element.
  82.    Also computes the entropy of the system.
  83.    */
  84. dims_count(data,xsize,ysize,ptotal,pentropy)
  85. double **data,*pentropy;
  86. int xsize,ysize,*ptotal;
  87. {
  88.     int i,j,total,equal,counter;
  89.     double entropy,log2();
  90.  
  91.            total = 1; /* first element is always unique */
  92.     counter = 1;
  93.     entropy=0;
  94.            for (i=0; i<xsize-1; i++) { /* do comparisons */
  95.                    equal = TRUE;
  96.                    for (j=0; equal && (j<ysize); j++) {
  97.                            if (((int) data[i][j]) != ((int) data[i+1][j])) 
  98.                 equal = FALSE;
  99.                            }
  100.                 if (!equal) {
  101.             ++total;
  102.             if (counter>1) {
  103.                 entropy += (counter*log2((double) counter));
  104.                 counter=1;
  105.                 }
  106.             }
  107.         else counter++;
  108.                    }
  109.     entropy += (counter*log2((double) counter));
  110.     *ptotal = total;
  111.     *pentropy=entropy;
  112. }
  113.  
  114.  
  115.